import json
import re
from abc import ABC, abstractmethod

import settings
from credit_voice_service.dialogue.service.build_data_service import BuildDataByFile, write_file, BuildDataByLLM, bot_document_to_tree, \
    BuildDataByLLM_copy, BuildDataByFileTwo
from credit_voice_service.dialogue.util import request_util
from credit_voice_service.settings import log


# 请求对话ID
class dialogueRequest(object):
    def __init__(self, session_id, workspace_id, dialogue_id, user_id):
        self.sessionId = session_id
        self.userId = user_id
        self.workspaceId = workspace_id
        self.dialogueId = dialogue_id

    def startForOutbound(self):
        url = "{}/startForOutbound?sessionId={}&workspaceId={}&userId={}&dialogueId={}"\
            .format(settings.DIALOGUE_URL, self.sessionId, self.workspaceId, self.userId, self.dialogueId)
        result = request_util.get(url, timeout=5)
        if result is not None:
            result = json.loads(result)
        log.info("[startForOutbound] result:{}".format(result))
        return result

    def process(self, query, is_try):
        url = "{}/process?sessionId={}&workspaceId={}&query={}&isTry={}" \
            .format(settings.DIALOGUE_URL, self.sessionId, self.workspaceId, query, is_try)
        result = request_util.get(url, timeout=5)
        log.info("[process] result:{}".format(result))
        return result

    def end(self):
        pass


class VerifyInterface(ABC):

    # 验证一轮对话
    def verifyDialogueRound(self, all_dialogue_info, session_id, workspace_id, dialogue_id):
        if all_dialogue_info is None:
            log.info("[verifyDialogueRound] 所有信息为空！")
            return
        dialogue_request = dialogueRequest(session_id, workspace_id, dialogue_id, session_id)
        for dialogue_info in all_dialogue_info:
            # todo 后面再考虑核身的首句
            queryStr = dialogue_info['请求话术']
            if queryStr is None or queryStr == "":
                continue
            # 1.请求开场
            start_content = dialogue_request.startForOutbound()
            # self.verifyStart(start_content, dialogue_info)
            # 2.请求对话过程
            processContent = ""
            queryList = queryStr.split(",")
            for userQuery in queryList:
                processContent = dialogue_request.process(userQuery, False)
            verify = self.verifyProcess(processContent, dialogue_info)
            dialogue_info['验证结果'] = verify
            dialogue_info['请求结果'] = processContent

            # 验证的数据写入文件中
        write_file(all_dialogue_info)

    # 验证开始流程 验证声模和流程
    def verifyStart(self, start_content, standard_info):
        print("111")
        pass

    # 验证中间流程
    def verifyProcess(self):
        pass


class Verify(VerifyInterface):

    def verifyStart(self, start_content, standard_info):
        # todo 校验开始信息 @liutao 录音编号、声模、标签
        print(start_content)
        print("1222")
        pass

    def verifyProcess(self, process_content, standard_info):
        log.info("[verifyProcess] 验证对话过程！")
        # pattern = r".*?@#(.+?)\|\|.+#@.*?"
        # if re.match(pattern, process_content):
        #     match = re.match(pattern, process_content)
        #     print("{}".format(match.group(1)))
        #     print("该文本格式正确")
        # else:
        #     print("该文本格式不正确")
        if standard_info is None or standard_info['录音编号'] is None or standard_info['录音编号'] == "":
            log.info("[verifyProcess] 无录音编号！")
            return "无录音"
        if standard_info['录音编号'] in process_content:
            return "已验证"
        else:
            return "错误"


if __name__ == '__main__':
    session_id = "0-2120-581717275-1673938085698949120-21"
    workspace_id = 1157115
    dialogue_id = 1179733

    buildDataByFile = BuildDataByFileTwo()
    dialogue_infos = buildDataByFile.buildData()
    verify_dialogue = Verify()
    verify_dialogue.verifyDialogueRound(dialogue_infos, session_id, workspace_id, dialogue_id)

    #
    # session_id = "0-2120-581717275-1673938085698949120-21"
    # workspace_id = 661490
    # dialogue_id = 1130824
    #
    # verify_dialogue = Verify()
    # verify_dialogue.verifyDialogueRound(dialogue_infos, session_id, workspace_id, dialogue_id)



    # dialogue_request = dialogueRequest(session_id, workspace_id, dialogue_id, session_id)
    # start = dialogue_request.startForOutbound()
    # isTry = False
    # query = "是的"
    # process = dialogue_request.process(query, isTry)

    # buildDataByFile = BuildDataByFile()
    # buildDataByFile = BuildDataByLLM()
    # dialogue_infos = buildDataByFile.buildData()

    # buildDataByFile = BuildDataByLLM_copy()
    # dialogue_infos = buildDataByFile.buildData()
    # # dialogue_infos = bot_document_to_tree(settings.dialogue_file_path)
    #
    # session_id = "0-2120-581717275-1673938085698949120-21"
    # workspace_id = 347
    # dialogue_id = 2235
    #
    # verify_dialogue = Verify()
    # verify_dialogue.verifyDialogueRound(dialogue_infos, session_id, workspace_id, dialogue_id)


    pass
